<%-- 
    Document   : manageplaylist
    Created on : Apr 1, 2013, 10:00:02 PM
    Author     : DinhNP
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Quản lý Playlists</title>
        <link href="css/manageplaylist.css" type="text/css" rel="stylesheet"/>
        <script type="text/javascript" >
            var allPlaylists;
            var listArtist;
            var listSong;

            function checkLogin(){
                if(sessionStorage.length==0){
                    window.location.href = "homePage.jsp";
                }
            }
            function loadUserPlaylistXML(){
                var xmlHttp = getXMLHttpRequest();

                xmlHttp.onreadystatechange=function(){
                    if (xmlHttp.readyState==4 && xmlHttp.status==200)
                    {
                        var xml = xmlHttp.responseXML;
                        if (xml!=null){
                            parser=new DOMParser();
                            xmlDoc=parser.parseFromString(xml,"text/xml");
                            playlists = xmlDoc.getElementById("Playlist");

                            for (var i=0;i<playlists.length;i++){
                                var playlistID="";
                                var playlistName="";
                                var username="";
                                var playlistCover="";
                                var listenNumber="";
                                var dateCreated="";
                                var playlistDescription="";
                                var playlistLikeNumber="";
                                
                                if (playlists[i].getElementsByTagName("PlaylistID")[0].firstChild!=null){
                                    playlistID = playlists[i].getElementsByTagName("PlaylistID")[0].firstChild.nodeValue.toString().trim();
                                }
                                if (playlists[i].getElementsByTagName("PlaylistName")[0].firstChild!=null){
                                    playlistName = playlists[i].getElementsByTagName("PlaylistName")[0].firstChild.nodeValue.toString().trim();
                                }
                                if (playlists[i].getElementsByTagName("PlaylistCover")[0].firstChild!=null){
                                    playlistCover = playlists[i].getElementsByTagName("PlaylistCover")[0].firstChild.nodeValue.toString().trim();
                                }
                                if (playlists[i].getElementsByTagName("ListenNumber")[0].firstChild!=null){
                                    listenNumber = playlists[i].getElementsByTagName("ListenNumber")[0].firstChild.nodeValue.toString().trim();
                                }
                                if (playlists[i].getElementsByTagName("DateCreated")[0].firstChild!=null){
                                    dateCreated = playlists[i].getElementsByTagName("DateCreated")[0].firstChild.nodeValue.toString().trim();
                                }
                                if (playlists[i].getElementsByTagName("PlaylistDescription")[0].firstChild!=null){
                                    playlistDescription = playlists[i].getElementsByTagName("PlaylistDescription")[0].firstChild.nodeValue.toString().trim();
                                }
                                if (playlists[i].getElementsByTagName("PlaylistLikeNumber")[0].firstChild!=null){
                                    playlistLikeNumber = playlists[i].getElementsByTagName("PlaylistLikeNumber")[0].firstChild.nodeValue.toString().trim();
                                }
                                var listSong = xmlDoc.getElementsByTagName("Songs");
                                for (var j=0; i<listSong.length;i++){
                                    var songID="";
                                    var songTitle="";
                                    var songSRC="";
                                    var lyrics="";
                                    var isMV="";
                                    var orderNumber="";

                                    if (playlists[i].getElementsByTagName("SongID")[0].firstChild!=null){
                                        songID = playlists[i].getElementsByTagName("SongID")[0].firstChild.nodeValue.toString().trim();
                                    }
                                    if (playlists[i].getElementsByTagName("SongTitle")[0].firstChild!=null){
                                        songTitle = playlists[i].getElementsByTagName("SongTitle")[0].firstChild.nodeValue.toString().trim();
                                    }
                                    if (playlists[i].getElementsByTagName("SongSRC")[0].firstChild!=null){
                                        songSRC = playlists[i].getElementsByTagName("SongSRC")[0].firstChild.nodeValue.toString().trim();
                                    }
                                    if (playlists[i].getElementsByTagName("Lyrics")[0].firstChild!=null){
                                        lyrics = playlists[i].getElementsByTagName("Lyrics")[0].firstChild.nodeValue.toString().trim();
                                    }
                                    if (playlists[i].getElementsByTagName("isMV")[0].firstChild!=null){
                                        isMV = playlists[i].getElementsByTagName("isMV")[0].firstChild.nodeValue.toString().trim();
                                    }
                                    if (playlists[i].getElementsByTagName("OrderNumber")[0].firstChild!=null){
                                        orderNumber = playlists[i].getElementsByTagName("OrderNumber")[0].firstChild.nodeValue.toString().trim();
                                    }

                                    listArtist =xmlDoc.getElementsByName("Artist");
                                    for (var k=0;k<listArtist.length;k++){
                                        var artistID="";
                                        var artistName="";

                                        if (playlists[i].getElementsByTagName("ArtistID")[0].firstChild!=null){
                                            artistID = playlists[i].getElementsByTagName("ArtistID")[0].firstChild.nodeValue.toString().trim();
                                        }
                                        if (playlists[i].getElementsByTagName("ArtistName")[0].firstChild!=null){
                                            artistName = playlists[i].getElementsByTagName("ArtistName")[0].firstChild.nodeValue.toString().trim();
                                        }

                                        listArtist.push({
                                            artistID:artistID,
                                            artistName:artistName
                                        });
                                    }

                                    listSong.push({
                                        songID:songID,
                                        songTitle:songTitle,
                                        songSRC:songSRC,
                                        lyrics:lyrics,
                                        isMV:isMV,
                                        orderNumber:orderNumber,
                                        listArtist:listArtist
                                    });

                                }

                                allPlaylists.push({
                                    playlistID: playlistID,
                                    playlistName: playlistName,
                                    username: username,
                                    playlistCover: playlistCover,
                                    listenNumber: listenNumber,
                                    dateCreated: dateCreated,
                                    listSong: listSong,
                                    playlistDescription: playlistDescription,
                                    playlistLikeNumber: playlistLikeNumber
                                });
                            }
                            
                        }
                    } // end if
                } // end function

                xmlHttp.open("GET","managePlaylists",true);
                xmlHttp.send();
            }
            function displayOnePlaylist(array){
                document.getElementsByName("txtPlaylistName")[0].value = array.playlistID;
                document.getElementsByName("txtDescription")[0].value = array.playlistName;
                document.getElementsByName("txtCreatedDate")[0].value = array.dateCreated;
                document.getElementsByName("txtDescription")[0].value = array.playlistDescription;


                var imgSRC= "db/"+sessionStorage.getItem("USER")+"/Playlist/"+array.playlistName+"/"+array.playlistCover;
                if (array.playlistCover == "" || !isFileExists(imgSRC)){
                    imgSRC = "img/DefaultAlbumCover.jpg"; //set Default playlist cover
                }
                document.getElementById("playlistCover").src = imgSRC;
                document.getElementsByName("txtListenNumber")[0].value = array.listenNumber;
                document.getElementsByName("txtLikeNumber")[0].value = array.playlistLikeNumber;



            }
        </script>
    </head>
    <body>
        <script>
            checkLogin();
        </script>
        <!-- Include TOP  -->
        <%@include file="masterpage_top.jsp" %>

        <!-- MAIN CONTENT  -->
        <div id="middle">
            <div id="middle_content">
                <div id="left_content">
                    <div class="left_content_header">THÔNG TIN PLAYLIST</div>
                    <div class="left_content_main">
                        <p>Tên playlist</p>
                        <input type="text" name="txtPlaylistName" required maxlength="70"/>

                        <p>Mô tả</p>
                        <textarea name="txtDescription" cols="50" rows="6"></textarea>

                        <p>Ngày tạo</p>
                        <input type="text" disabled name="txtCreatedDate"/>


                        <p>Ảnh bìa</p><img id="playlistCover" /><br/>
                        <input type="file" name="fileCover"/>

                        <p>Số lượt nghe</p>
                        <input type="text" disabled name="txtListenNumber"/>
                        <p>Số lượt thích</p>
                        <input type="text" disabled name="txtLikeNumber"/>

                        <button type="submit">Cập nhật</button>      
                        <button type="button">Xóa Playlist</button>
                    </div>
                </div>
                <div id="right_content" >
                    <div class="right_content_header" >THÔNG TIN CÁ NHÂN</div>
                    <div class="right_content_main">
                    </div>
                </div>
            </div>
        </div>

        <!-- Include BOTTOM  -->
        <%@include file="masterpage_bottom.jsp" %>
    </body>
</html>
